//
//  XS_m
//  AudioVideo
//
//  Created by xueshan1 on 2019/8/23.
//  Copyright © 2019 xueshan1. All rights reserved.
//

#import "XSShootingButton.h"

static CGFloat EnlargedLineWidth = 40;
 
@interface XSShootingButton ()

@property(nonatomic,strong)CAShapeLayer *centerLayer;
@property(nonatomic,strong)CAShapeLayer *progressLayer;
@property(nonatomic,strong)CADisplayLink *displayLink;
@property(nonatomic,assign)CGFloat progressValue;

@end

@implementation XSShootingButton

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
//        [self setupLayer];
    }
    return self;
}


- (void)setupLayer{
    [self.layer addSublayer:self.centerLayer];
    [self.layer addSublayer:self.progressLayer];
    
    //长按
    UILongPressGestureRecognizer * longTap = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longClick:)];
    longTap.minimumPressDuration = 0.3;
    [self addGestureRecognizer:longTap];
    
    UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(singleTapGesture)];
    [self addGestureRecognizer:tapGes];
    
    [tapGes requireGestureRecognizerToFail:longTap];
    
}
- (CAShapeLayer *)centerLayer{
    if (!_centerLayer) {
        _centerLayer = [[CAShapeLayer alloc]init];
        _centerLayer.frame = CGRectInset(self.bounds, 0, 0);
        _centerLayer.strokeColor = [UIColor colorWithHexString:@"#dad9d6"].CGColor;
        _centerLayer.fillColor = [UIColor whiteColor].CGColor;
        _centerLayer.lineWidth = 10;
        _centerLayer.path = [UIBezierPath bezierPathWithOvalInRect:_centerLayer.bounds].CGPath;
    }
    return _centerLayer;
}
- (CAShapeLayer *)progressLayer{
    if (!_progressLayer) {
        _progressLayer = [[CAShapeLayer alloc]init];
        _progressLayer.frame = self.bounds;
        _progressLayer.strokeColor   = [UIColor colorWithHexString:@"#1aad19"].CGColor;
        _progressLayer.fillColor     = [UIColor clearColor].CGColor;
        _progressLayer.lineWidth     = 5;
        _progressLayer.strokeStart   = 0;
        _progressLayer.strokeEnd     = 0;
        _progressLayer.path      = [UIBezierPath bezierPathWithOvalInRect:CGRectInset(self.bounds, -EnlargedLineWidth/2 + 2.5, -EnlargedLineWidth/2 + 2.5)].CGPath;
        CATransform3D turnTrans = CATransform3DMakeRotation(-M_PI / 2, 0, 0, 1);
        _progressLayer.transform= turnTrans;
    }
    return _progressLayer;
}

- (void)layoutSubviews{
    [super layoutSubviews];
 
}

/**
 长按事件
 */
-(void)longClick:(UILongPressGestureRecognizer*)tap{
    if (tap.state == UIGestureRecognizerStateBegan) {
        self.centerLayer.lineWidth = EnlargedLineWidth;
        self.progressLayer.strokeColor = [UIColor greenColor].CGColor;
        self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(changeProgerss)];
        [self.displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
        if (self.longPressDelegate && [self.longPressDelegate respondsToSelector:@selector(shootingButton:longPressStart:)]) {
            [self.longPressDelegate shootingButton:self longPressStart:self.progressValue];
        }
    }else if (tap.state == UIGestureRecognizerStateEnded ||
              tap.state == UIGestureRecognizerStateCancelled){
        [self stopProress];
    }
    
}
/**
 修改进度要的状态（定时器实时调用的方法）
 */
-(void)changeProgerss
{
    double speed = (1.0/10.0)/60.0;//拍摄的时间
    self.progressValue = self.progressValue + speed;
    self.progressLayer.strokeEnd = self.progressValue;
    if (self.progressValue >= 1.05) {
        [self stopProress];
    }
    
}
/**
 结束视频的拍摄
 */
-(void)stopProress{
    if (self.longPressDelegate && [self.longPressDelegate respondsToSelector:@selector(shootingButton:longPressEnd:)]) {
        [self.longPressDelegate shootingButton:self longPressEnd:self.progressValue];
    }
    
    self.progressValue = 0;
    self.progressLayer.strokeEnd =   self.progressValue;
    self.progressLayer.strokeColor = [UIColor clearColor].CGColor;
    [self.displayLink invalidate];
    self.centerLayer.lineWidth = 10;

}

- (void)singleTapGesture{
    if (self.longPressDelegate && [self.longPressDelegate respondsToSelector:@selector(shootingButtonSingleTap:)]) {
        [self.longPressDelegate shootingButtonSingleTap:self];
    }
}


@end
